package arduino;

import java.util.Arrays;


public class AProtocol {

	/* input */
	public int in_angle = 0;
	public int in_direction = 0;
	public int in_vel = 0;
	public int in_beep_hz = 0;
	public int in_beep_msec = 0;
	
	/* output */
	public int out_vel=0;
	public int out_angle = 0;
	public int out_light=0;
	public int out_sensor_1=0;
	
	
	public static String separator = ";";
	
	
	
	public AProtocol(){};

	
	public AProtocol(int in_angle, int in_direction, int in_vel,
			int in_beep_hz, int in_beep_msec, int out_vel, int out_angle,
			int out_light, int out_sensor_1) {
		super();
		this.in_angle = in_angle;
		this.in_direction = in_direction;
		this.in_vel = in_vel;
		this.in_beep_hz = in_beep_hz;
		this.in_beep_msec = in_beep_msec;
		this.out_vel = out_vel;
		this.out_angle = out_angle;
		this.out_light = out_light;
		this.out_sensor_1 = out_sensor_1;
	}
	public static AProtocol parse(String str)
	{
		
		if(str==null)
			return null;
		try{
		String []array = str.trim().split(separator);
		System.out.println(array.length);
		System.out.println(Arrays.toString(array));
		return new AProtocol(
				Integer.parseInt(array[0]), 
				Integer.parseInt(array[1]),
				Integer.parseInt(array[2]),
				Integer.parseInt(array[3]),
				Integer.parseInt(array[4]),
				Integer.parseInt(array[5]), 
				Integer.parseInt(array[6]),
				Integer.parseInt(array[7]),
				Integer.parseInt(array[8])			
				);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public int[] toArray()
	{
		return new int[]{in_angle,in_direction,in_vel,in_beep_hz,in_beep_msec,out_vel,out_angle,out_sensor_1,out_light};
	}
	
	@Override
	public String toString() {
		int [ ]array = toArray();
		String str ="";
		for(int i =0 ;i<array.length;i++)
		{
			str+=array[i]+separator;
		}
		
		return str.substring(0,str.length()-separator.length());
	}
	
}